package leetcode.editor.cn.q1xx

import kotlin.math.min
import kotlin.math.max

class Q121 {
    fun maxProfit(prices: IntArray): Int {
        var maxProfit = 0
        val minPrice = IntArray(prices.size)
        for (i in prices.indices) {
            if (i == 0) {
                minPrice[0] = prices[0]
            } else {
                minPrice[i] = min(minPrice[i-1],prices[i])
            }
            maxProfit = max(maxProfit, prices[i]-minPrice[i-1])
        }
        return maxProfit
    }
}